Ext.onReady(function() {
    Star.DEBUG = true;
    var def = column_info;
    var listDivId = 'main';
    var pjName="";var tbName="";
    
    var query = function(d) {
        var projectName = Ext.getCmp('q_projectName');
        var tableName = Ext.getCmp('q_tableName');
        var filter = "1=1";
        if (projectName != "undefined" && projectName != null) {
            if (projectName.getValue() != ""){
                filter = filter + "&projectName=" + projectName.getValue();
                pjName=projectName.getValue();
            }
        }
        if (tableName != "undefined" && tableName != null) {
            if (tableName.getValue() != ""){
                filter = filter + "&tableName=" + tableName.getValue();
                tbName=tableName.getValue();
            }
        }
        d.baseParams = {
            s_method: 'list',
            query: filter,
            limit: 20
        };
        //alert(filter);
        return true;
    };

    var columnGrid = new MyEditGrid({/* applyTo:listDivId, */
        id: "columnGrid",
        //layout : 'fit',
        width: 700,
        height: 500,
        applyTo: listDivId,
        ex_buttonsToRight: true,
        ex_displayInfo: true,
        ex_queryCfg: def.getQueryConfig(),
        ex_datacfg: {
            metas: def.metas,
            pageSize: 20,
            facade: def.facade,
            sortField: 'projectName',
            sortDir: 'DESC',
            query: query
        }
    });
    columnGrid.Load();
    
    //监听编辑后事件
    columnGrid.on("afteredit", afterEdit, columnGrid); 
    function afterEdit(obj) {
        var r = obj.record; //获取被修改的行
        var l = obj.field; //获取被修改的列
        var id = r.get("id");
        if (l == "ad_jkrq")
            var lie = Ext.util.Format.date(r.get(l), "Y-m-d");
        else
            var lie = r.get(l);
        Ext.Ajax.request({
            url: def.facade,
            params: {
                s_method: 'save',
                id: id,
                data: Ext.encode(r.data)
            },
            success: function(response) {
                columnGrid.getStore().commitChanges();
            },
            failure: function(response) {
                alert("保存失败！");
            }
        });
    } 
    
    function save() {
        fp.SaveData();
        fp.form.reset();
        win.hide()
    }

    var saveRecord = new Ext.Action({
        text: Star_Info.SAVE,
        iconCls: 'crud-save',
        tooltip: Star_Info.SAVE_TIP,
        handler: save
    });
    var closeAction = new Ext.Action({
        text: '关闭',
        iconCls: 'crud-reset',
        tooltip: '关闭',
        handler: function() {
            win.hide()
        }
    });
    var fp = new MyFormPanel({
        id: 'fp1',
        height: 350,
        //padding:"50",
        ex_facade: def.facade,
        ex_BeforeSave: check,
        ex_SubmitSuccess: notify,
        ex_SubmitFailed: notify,
        ex_metas: def.metas,
        ex_groups: [def.group1], //多个逗号隔开
        ex_defaultRec: def.newRecord()
    });

    var win = new Ext.Window({
        title: '项目数据库维护',
        width: 300,
        height: 300,
        //closable:false,//关闭按钮
        draggable: true,
        //layout : 'fit',
        //plain : true,
        autoDestroy : true,
        modal: true,
        resizable: false,
        buttonAlign: 'center',
        closeAction: 'hide',
        resizable: false,
        items: fp,
        buttons: [saveRecord, closeAction]
    })
    
    columnGrid.getTopToolbar().addButton({
        text: '新增',
        iconCls: "table_add",
        handler: function() {
            fp.LoadById('N/A');
            win.show();
        }
    });

    columnGrid.getTopToolbar().addButton({
        text: '编辑',
        iconCls: "table_edit",
        handler: function() {
            var record = columnGrid.getSelectionModel().getSelections();
            if (record.length == 1) {
                win.show();
                showSelect(record[0].get('id'));
            }
            else {
                Ext.MessageBox.alert("错误！", "只能选中一条记录！");
            }
        }
    });

    columnGrid.getTopToolbar().addButton({
        text: '批量删除',
        iconCls: "table_delete",
        handler: function() {
            var rs = columnGrid.GetSelectedRecords();
            if (checkSelects(rs.length)) {
                Ext.MessageBox.confirm(Star_Info.BEFORE_DEL_ALL, Star_Info.BEFORE_DEL2, function(btn) {
                    if (btn == 'yes') {
                        for (var i = 0; i < rs.length; i++) {
                            Star.deleteRecord(rs[i], {
                                url: def.facade,
                                beforeDelete: function() {
                                    return true;
                                },
                                success: function() {
                                    columnGrid.Load();
                                },
                                failure: function() {
                                    Ext.MessageBox.alert("错误", "出错了!");
                                }
                            });
                        }
                    }
                });
            }
        }
    });
    
    columnGrid.getTopToolbar().addButton({
        text: '生成',
        iconCls: "table_add",
        handler: function() {
            createFiles();
        }
    });
    
    function createFiles(){
        if(Ext.getCmp('q_projectName').getValue() != "" || Ext.getCmp('q_tableName').getValue() !="")
        {
            Ext.Ajax.request({
			    url : '/CF.aspx',
			    params : {
				    pjName :Ext.getCmp('q_projectName').getValue(),
				    tbName : Ext.getCmp('q_tableName').getValue()
			    },
			    success : function(response) {
			        Ext.Msg.alert('提示','文件已成功生成');
			    },
			    failure : function(response) {
				    Ext.Msg.alert(Star_Info.SAVE_FAIL,response.responseText);
			    }
		    });
		}
		else{
		    Ext.Msg.alert('错误','项目名称及表格名称不能为空！');
		}
    }
    
    function showSelect(id) {
        fp.LoadById(id);
    }

    function notify(data, msg) {
        columnGrid.Load();
        //fp.LoadById('N/A');
    }

    function check(data) {
        // Star.log(data);
        return true; // false will cacel the submit!
    }
    
});